//index.js
var app = getApp();
// pages/my/complaints/index.js
Page({
  data:{
    goodsItem: [],
    tempFilePathsArray: [],
    tempFileTruePathContainer: [],
    tempFileUpload: [
    ],
    complaintsComment: {
      anonymous: 0
    }


  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.goodsId = parseInt(options.Id || 0);

  },
  onReady: function () {
    // 页面渲染完成
    this.loadData(this.goodsId);
    this.setData({
      'complaintsComment.goodsId': this.goodsId
    });
  },
  loadData: function (goodsId){
    var that = this;
    app.doRequest('getGoods', {
      goodsId: goodsId
    }, function (data) {
      console.log(data);
      that.setData({
        goodsItem: data
      });
    })
  },
  uploadImage:function (){
    var that = this;
    if(that.data.tempFileUpload.length ==5){
      wx.showModal({
        title: '提示',
        content: '最多只能上传五张图片!',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
          }
        }
      })
    }else{
      wx.chooseImage({
        count: 5,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          console.log(res);
          var tempFileUploadArray = that.data.tempFileUpload;


          var tempFilePathsContain = res.tempFilePaths;

          tempFilePathsContain.forEach(function(value,index,array){
            tempFileUploadArray.push({
              tempFileLocalPath: value,
              tempFileTruePath: ''
            });
          });

          that.setData({
            tempFileUpload: tempFileUploadArray
          });
         if(that.data.tempFileUpload.length>5){
           wx.showModal({
             title: '提示',
             content: '只允许上传五张图片!',
             showCancel: false,
             success: function(res) {
               if (res.confirm) {
                 var tempFileUploadDlt = that.data.tempFileUpload.splice(0,5);
                 that.setData({
                   tempFileUpload: []
                 });
                 that.setData({
                   tempFileUpload: tempFileUploadDlt
                 });

               }
             }
           });
         }
          that.getTempFileTruePaths(that.data.tempFileUpload,0,
              function(tempFileItems){
                console.log(tempFileItems);
                that.setData({
                  tempFileUpload: tempFileItems
                });
              },
              function(tempFileItems){
                wx.showModal({
                  title: '提示',
                  content: '图片上传失败!',
                  showCancel: false,
                  success: function(res) {
                    if (res.confirm) {
                      that.setData({
                        tempFileUpload: tempFileItems
                      });
                    }
                  }
                })
              }
          );




        }
      })
    }

  },
  submitComplaintsMsg: function (){

    var that = this;
    var complaintsCommentTxt = that.data.complaintsComment.content;
    if(!complaintsCommentTxt){

      wx.showModal({
        title: '提示',
        content: '评论不能为空!',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
          }
        }
      })
    } else {


      var tempFileUploadList = that.data.tempFileUpload;
          that.setData({
            'complaintsComment.image1': tempFileUploadList[0] ? tempFileUploadList[0].tempFileTruePath : '',
            'complaintsComment.image2': tempFileUploadList[1] ? tempFileUploadList[1].tempFileTruePath : '',
            'complaintsComment.image3': tempFileUploadList[2] ? tempFileUploadList[2].tempFileTruePath : '',
            'complaintsComment.image4': tempFileUploadList[3] ? tempFileUploadList[3].tempFileTruePath : '',
            'complaintsComment.image5': tempFileUploadList[4] ? tempFileUploadList[4].tempFileTruePath : ''
          });

      app.doRequest('goodsComment', that.data.complaintsComment, function (data) {
        //console.log(data);
        app.showToastMsg('提交成功',function (){
          wx.navigateBack({delta:1});
        });
      });
    }
  },
  bindTextAreaFocus: function(event){

    this.data.complaintsComment.content = event.detail.value;
    this.setData({
      'complaintsComment.complaintsText': event.detail.value
    })
  },
  bindTextAreaBlur: function(event){
    this.data.complaintsComment.content = event.detail.value;
    this.setData({
      'complaintsComment.content': event.detail.value
    })
  },
  checkboxChange: function(event) {
    //console.log(event.detail.value)
    this.setData({
      'complaintsComment.anonymous': event.detail.value[0]
    });
  },
  complaintsImagePreview: function(event) {
    var that = this;
    var truePathIndex = parseInt(event.currentTarget.dataset.truepathindex);

    that.data.tempFileUpload.splice(truePathIndex,1);
    var tempFileUploadDelete = that.data.tempFileUpload;
    that.setData({
      tempFileUpload:[]
    });
    that.setData({
     tempFileUpload: tempFileUploadDelete
    });
    app.showToastMsg('删除成功');
    //setTimeout(function(){
    //  wx.showToast({
    //    title: '删除成功',
    //    icon: 'success',
    //    duration: 1000
    //  });
    //},300);
  },
  getTempFileTruePaths: function(tempFilePaths,index,callbackSuccess,callbackFail){
    var that = this;
    app.doUpload(tempFilePaths[index].tempFileLocalPath, function (data) {
      // 成功，返回图片URL
      tempFilePaths[index].tempFileTruePath = data;
      console.log(tempFilePaths);
      if(tempFilePaths[index].tempFileTruePath) {
        if (typeof callbackSuccess === 'function') {
          callbackSuccess && callbackSuccess(tempFilePaths)
        }
      }else{
        if (typeof callbackFail === 'function') {
          callbackFail && callbackFail(tempFilePaths)
        }
      }
      if(index < tempFilePaths.length-1){
        that.getTempFileTruePaths(tempFilePaths,index+1,callbackSuccess,callbackFail);
      }else{
        console.log('图片上传完成');
      }

    }, function (code) {
      // 失败处理
      console.log(code);
    })
    }


})